package com.dbq.fast.business.invite.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dbq.fast.R;
import com.dbq.fast.business.invite.been.ShareItem;

import java.util.List;

/**
 * Created by yuanchen on 2018/5/17.
 */

public class ShareAdapter extends RecyclerView.Adapter{

    List<ShareItem> shareItems;

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_share, parent, false);
        return new ShareItemViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ShareItemViewHolder shareItemVH = (ShareItemViewHolder) holder;
        ShareItem shareItem = shareItems.get(position);
        shareItemVH.icon.setImageResource(shareItem.getIcon());
        shareItemVH.title.setText(shareItem.getTitle());
        shareItemVH.itemView.setOnClickListener(view -> {
            if(iItemClickListener != null){
                iItemClickListener.onItemClick(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return shareItems == null ? 0: shareItems.size();
    }

    class ShareItemViewHolder extends RecyclerView.ViewHolder{

        ImageView icon;
        TextView title;

        public ShareItemViewHolder(View itemView) {
            super(itemView);

            icon = itemView.findViewById(R.id.icon);
            title = itemView.findViewById(R.id.title);
        }
    }

    public void setData(List<ShareItem> shareItems){
        this.shareItems = shareItems;
        notifyDataSetChanged();
    }

    private IItemClickListener iItemClickListener;
    public void setItemClickListener(IItemClickListener itemClickListener){
        this.iItemClickListener = itemClickListener;
    }

}
